package com.codemonkey.lq.test.sampledata.deprecated;

import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;

public class PersonDeprecatedCanCompare extends PersonDeprecated implements
		Comparable<PersonDeprecatedCanCompare> {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private int age;

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public static List<PersonDeprecatedCanCompare> getPersonCanCompareGroup() {
		ArrayList<PersonDeprecatedCanCompare> persons = CollectionUtil.newArrayList();
		persons.add(new PersonDeprecatedCanCompare() {
			{
				this.setFirstName("One");
				this.setLastName("OneEnd");
				this.setAge(11);
			}
		});
		persons.add(new PersonDeprecatedCanCompare() {
			{
				this.setFirstName("Two");
				this.setLastName("TwoEnd");
				this.setAge(2);
			}
		});
		persons.add(new PersonDeprecatedCanCompare() {
			{
				this.setFirstName("Three");
				this.setLastName("ThreeEnd");
				this.setAge(33);
			}
		});
		persons.add(new PersonDeprecatedCanCompare() {
			{
				this.setFirstName("Four");
				this.setLastName("FourEnd");
				this.setAge(4);
			}
		});
		persons.add(new PersonDeprecatedCanCompare() {
			{
				this.setFirstName("Five");
				this.setLastName("FiveEnd");
				this.setAge(5);
			}
		});

		return persons;
	}

	@Override
	public String toString() {
		return ObjectUtil.toString(BeanUtil.beanToMap(this));
	}

	public int compareTo(PersonDeprecatedCanCompare other) {
		// 参见Spring中的AbstractBeanDefinition类

		if (this == other) {
			return 0;
		}

		// 《改善Java程序的151个建议》建议47 使用getClass进行类型判断  ~ P77
		//	if (!(other instanceof Tester)) { // instanceof 有继承关系都会返回true.可能违反自反性
		//		return false;
		//	}
		if (other == null || other.getClass() != this.getClass()) {
			return -1;
		}

		PersonDeprecatedCanCompare that = (PersonDeprecatedCanCompare) other;

		return this.age - other.age;
	}

}
